﻿Imports Microsoft.VisualBasic

Public Class clsMoney

    Private strMsg As String = ""
    Private strError As String = ""
    Public ReadOnly Property getStrMsg() As String
        Get
            Return strMsg
        End Get

    End Property
    Public ReadOnly Property getStrError() As String
        Get
            Return strError
        End Get

    End Property
    Public Function insertComma(ByVal strTransAmt As String) As String
        Dim dcmMoney As Decimal
        Try
            dcmMoney = strTransAmt
            strTransAmt = Decimal.Parse(strTransAmt).ToString("N2")
        Catch ex As InvalidCastException
            strMsg = "กรุณากรอกข้อมูลเป็นตัวเลข"
            Throw ex
        Catch ex As Exception
            strError = "เกินข้อผิดพลาด"
            Throw ex
        End Try
        Return strTransAmt
    End Function

    Public Function deleteComma(ByVal strTransAmt As String) As String
        Dim dcmMoney As Decimal
        Try
            If strTransAmt = "" Then
                strTransAmt = "0"
            End If
            dcmMoney = strTransAmt
            strTransAmt = dcmMoney
        Catch ex As InvalidCastException
            strMsg = "กรุณากรอกข้อมูลเป็นตัวเลข"
            Throw ex
        Catch ex As Exception
            strError = "เกินข้อผิดพลาด"
            Throw ex
        End Try
        Return strTransAmt
    End Function

    Public Function chkDataTransAmt(ByVal strTransAmt As String) As Boolean
        Dim ckktranAmt As Boolean = False
        Dim dcmMoney As Decimal
        Try
            dcmMoney = strTransAmt
            ckktranAmt = True
        Catch ex As InvalidCastException
            strMsg = "กรุณากรอกข้อมูลเป็นตัวเลข"
            Throw ex
        Catch ex As Exception
            strError = "เกินข้อผิดพลาด"
            Throw ex
        End Try
        Return ckktranAmt
    End Function

    '/////////////////////////////////////////////////////////////////////////////////////////////
    ' ///                                                             Change Data To Thai Baht
    '/////////////////////////////////////////////////////////////////////////////////////////////

    '*** ทำการแปลงจำนวนเงินที่เป็นตัวเลขให้เป็นตัวหนังสือ (ภาษาไทย) '
    Private Function Money2StrTha(ByVal Money As String) As String
        '===================================================='
        ' '
        ' ทำการแปลงตัวเลขให้เป็นตัวหนังสือ (ภาษาไทย) '
        ' '
        '===================================================='
        Dim T_Result As String
        Dim I, MCol As Integer
        Dim IntChr(9) As String
        Dim ColMon(7) As String

        IntChr(1) = "หนึ่ง" : IntChr(2) = "สอง"
        IntChr(3) = "สาม" : IntChr(4) = "สี่"
        IntChr(5) = "ห้า" : IntChr(6) = "หก"
        IntChr(7) = "เจ็ด" : IntChr(8) = "แปด"
        IntChr(9) = "เก้า"

        ColMon(0) = "แสน" : ColMon(1) = "ล้าน"
        ColMon(2) = "สิบ" : ColMon(3) = "ร้อย"
        ColMon(4) = "พัน" : ColMon(5) = "หมื่น"

        T_Result = ""
        MCol = Len(Money)
        For I = 1 To Len(Money)
            If (Mid(Money, I, 1) <> "0") And (Mid(Money, I, 1) <> "") Then
                If (MCol Mod 6 = 2) And IntChr(Val(Mid(Money, I, 1))) = "สอง" Then
                    T_Result = T_Result & "ยี่" & ColMon(MCol Mod 6)
                ElseIf MCol = 1 Then
                    If IntChr(Val(Mid(Money, I, 1))) = "หนึ่ง" And Len(Money) <> 1 Then
                        T_Result = T_Result & "เอ็ด" & ""
                    Else
                        T_Result = T_Result & IntChr(Val(Mid(Money, I, 1))) & ""
                    End If
                ElseIf (MCol Mod 6 = 1) And (Len(Money) > 7) And (IntChr(Val(Mid(Money, I, 1))) = "หนึ่ง") Then
                    T_Result = T_Result & "เอ็ด" & ColMon(MCol Mod 6)
                ElseIf (MCol Mod 6 = 2) And IntChr(Val(Mid(Money, I, 1))) = "หนึ่ง" Then
                    T_Result = T_Result & ColMon(MCol Mod 6)
                Else
                    T_Result = T_Result & IntChr(Val(Mid(Money, I, 1))) & ColMon(MCol Mod 6)
                End If
            End If
            If (MCol = 7) And (Mid(Money, I, 1) = "0") Then T_Result = T_Result & "ล้าน"
            MCol = MCol - 1
        Next I

        If T_Result = "" Then
            T_Result = "ศูนย์"
        End If

        Money2StrTha = T_Result

    End Function

    '*** แปลงหน่วยเป็นบาท(ภาษาไทย) '
    Public Function ThaStrMoney(ByVal Money As String) As String
        Dim T_Result As String
        Dim T_Money As Long
        Dim T_Stang As Integer

        If Not IsNumeric(Money) Then
            Return ""
        End If

        If InStr(Money, ".") <> 0 Then
            T_Money = Left(Money, InStr(Money, ".") - 1)
            T_Stang = Right(Money, Len(Money) - InStr(Money, "."))
            T_Result = Money2StrTha(T_Money)
            If Val(T_Stang) = 0 Then
                T_Result = T_Result & "บาทถ้วน"
            Else
                T_Result = T_Result & "บาท " & Money2StrTha(Trim(Str(Val(T_Stang)))) & "สตางค์"
            End If
        Else
            T_Result = Money2StrTha(Money) & "บาทถ้วน"
        End If
        ThaStrMoney = T_Result
    End Function

End Class
